function varargout = SelectManualScales(varargin)
% SELECTMANUALSCALES_FIGURE M-file for SelectManualScales_figure.fig
%      SELECTMANUALSCALES_FIGURE, by itself, creates a new SELECTMANUALSCALES_FIGURE or raises the existing
%      singleton*.
%
%      H = SELECTMANUALSCALES_FIGURE returns the handle to a new SELECTMANUALSCALES_FIGURE or the handle to
%      the existing singleton*.
%
%      SELECTMANUALSCALES_FIGURE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SELECTMANUALSCALES_FIGURE.M with the given input arguments.
%
%      SELECTMANUALSCALES_FIGURE('Property','Value',...) creates a new SELECTMANUALSCALES_FIGURE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SelectManualScales_figure_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SelectManualScales_figure_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help SelectManualScales_figure

% Last Modified by GUIDE v2.5 22-Apr-2005 13:40:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SelectManualScales_figure_OpeningFcn, ...
                   'gui_OutputFcn',  @SelectManualScales_figure_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before SelectManualScales_figure is made visible.
function SelectManualScales_figure_OpeningFcn(hObject, eventdata, handles, varargin)
global Project
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SelectManualScales_figure (see VARARGIN)

handles.xrange = varargin{3};
handles.yrange = varargin{4};
handles.PrintDataType = varargin{5};

% Choose default command line output for SelectManualScales_figure
handles.output = hObject;

% Initialize the text fields in figure
set(handles.DataXMin_text, 'String',num2str(varargin{3}(1)));
set(handles.DataXMax_text, 'String',num2str(varargin{3}(2)));
set(handles.DataYMin_text, 'String',num2str(varargin{4}(1)));
set(handles.DataYMax_text, 'String',num2str(varargin{4}(2)));
set(handles.PlotXMin_edittext, 'String',num2str(varargin{3}(1)));
set(handles.PlotXMax_edittext, 'String',num2str(varargin{3}(2)));
set(handles.PlotYMin_edittext, 'String',num2str(varargin{4}(1)));
set(handles.PlotYMax_edittext, 'String',num2str(varargin{4}(2)));
if (varargin{1} == 0) % X Plot scale flag set to autoscale
    set(handles.PlotXMin_text, 'Enable','Off');
    set(handles.PlotXMin_edittext,'Enable','Off');
    set(handles.PlotXMax_text,'Enable','Off');
    set(handles.PlotXMax_edittext,'Enable','Off');
end
if (varargin{2} == 0) % Y Plot scale flag set to autoscale
    set(handles.PlotYMin_text, 'Enable','Off');
    set(handles.PlotYMin_edittext,'Enable','Off');
    set(handles.PlotYMax_text,'Enable','Off');
    set(handles.PlotYMax_edittext,'Enable','Off');
end

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SelectManualScales_figure wait for user response (see UIRESUME)
uiwait(handles.SelectManualScales_figure);

% --- Outputs from this function are returned to the command line.
function varargout = SelectManualScales_figure_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

varargout = {[handles.xrange], [handles.yrange], handles.option};

% XManualScale = handles.xrange;
% YManualScale = handles.yrange;
% status = handles.option;

delete(hObject);


% --- Executes during object creation, after setting all properties.
function PlotXMin_edittext_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PlotXMin_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function PlotXMin_edittext_Callback(hObject, eventdata, handles)
% hObject    handle to PlotXMin_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PlotXMin_edittext as text
%        str2double(get(hObject,'String')) returns contents of PlotXMin_edittext as a double

% Check validity of value entered
xmin = str2num(get(handles.PlotXMin_edittext,'String'));
if ~isempty(str2num(get(handles.PlotXMax_edittext,'String')))
	xmax = str2num(get(handles.PlotXMax_edittext,'String'));
else
    xmax = handles.xrange(2);
end
switch handles.PrintDataType
    case {0,1} % Time History
        if xmin < 0
            set(handles.PlotXMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        elseif xmin > xmax
            set(handles.PlotXMin_edittext, 'BackgroundColor','red');
            set(handles.PlotXMax_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
        else
            set(handles.PlotXMin_edittext, 'BackgroundColor','white');
        end
    case {2,3} % Analysis or TH & Analysis
        if (xmin <=0)
            set(handles.PlotXMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        elseif xmin > xmax
            set(handles.PlotXMin_edittext, 'BackgroundColor','red');
            set(handles.PlotXMax_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
        else
            set(handles.PlotXMin_edittext,'BackgroundColor','white');
        end
end
if ((isequal(get(handles.PlotXMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotXMax_edittext,'BackgroundColor'),[1,1,1])) & ...
        (isequal(get(handles.PlotYMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotYMax_edittext,'BackgroundColor'),[1,1,1])));
    set(handles.Ok_pushbutton,'Enable','On');
end

handles.xrange(1) = str2num(get(handles.PlotXMin_edittext,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function PlotXMax_edittext_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PlotXMax_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function PlotXMax_edittext_Callback(hObject, eventdata, handles)
% hObject    handle to PlotXMax_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PlotXMax_edittext as text
%        str2double(get(hObject,'String')) returns contents of PlotXMax_edittext as a double

% Check validity of value entered
xmax = str2num(get(handles.PlotXMax_edittext,'String'));
if ~isempty(str2num(get(handles.PlotXMin_edittext,'String')))
	xmin = str2num(get(handles.PlotXMin_edittext,'String'));
else
    xmin = handles.xrange(1);
end
if (xmax < xmin)
    set(handles.PlotXMax_edittext, 'BackgroundColor','red');
    set(handles.PlotXMin_edittext, 'BackgroundColor','red');
    set(handles.Ok_pushbutton,'Enable','Off');
    return
else
    set(handles.PlotXMax_edittext, 'BackgroundColor','white');
end

if ((isequal(get(handles.PlotXMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotXMax_edittext,'BackgroundColor'),[1,1,1])) & ...
        (isequal(get(handles.PlotYMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotYMax_edittext,'BackgroundColor'),[1,1,1])));
    set(handles.Ok_pushbutton,'Enable','On');
end

handles.xrange(2) = str2num(get(handles.PlotXMax_edittext,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function PlotYMin_edittext_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PlotYMin_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function PlotYMin_edittext_Callback(hObject, eventdata, handles)
% hObject    handle to PlotYMin_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PlotYMin_edittext as text
%        str2double(get(hObject,'String')) returns contents of PlotYMin_edittext as a double

% Check validity of value entered
ymin = str2num(get(handles.PlotYMin_edittext,'String'));
if ~isempty(str2num(get(handles.PlotYMax_edittext,'String')))
	ymax = str2num(get(handles.PlotYMax_edittext,'String'));
else
    ymax = handles.yrange(2);
end
switch handles.PrintDataType
    case {0,1} % Time History
        if ymin > ymax
            set(handles.PlotYMax_edittext, 'BackgroundColor','red');
            set(handles.PlotYMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        else
            set(handles.PlotYMin_edittext, 'BackgroundColor','white');
        end
    case {2,3} % Analysis or TH & Analysis
        if ymin <= 0
            set(handles.PlotYMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
        elseif ymax < ymin
            set(handles.PlotYMax_edittext, 'BackgroundColor','red');
            set(handles.PlotYMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        else
            set(handles.PlotYMin_edittext, 'BackgroundColor','white');
        end
end
if ((isequal(get(handles.PlotXMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotXMax_edittext,'BackgroundColor'),[1,1,1])) & ...
        (isequal(get(handles.PlotYMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotYMax_edittext,'BackgroundColor'),[1,1,1])));
    set(handles.Ok_pushbutton,'Enable','On');
end

handles.yrange(1) = str2num(get(handles.PlotYMin_edittext,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function PlotYMax_edittext_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PlotYMax_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function PlotYMax_edittext_Callback(hObject, eventdata, handles)
% hObject    handle to PlotYMax_edittext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PlotYMax_edittext as text
%        str2double(get(hObject,'String')) returns contents of PlotYMax_edittext as a double

% Check validity of value entered
ymax = str2num(get(handles.PlotYMax_edittext,'String'));
if ~isempty(str2num(get(handles.PlotYMin_edittext,'String')))
	ymin = str2num(get(handles.PlotYMin_edittext,'String'));
else
    ymin = handles.yrange(1);
end
switch handles.PrintDataType
    case {0,1} % Time History
        if ymin > ymax
            set(handles.PlotYMax_edittext, 'BackgroundColor','red');
            set(handles.PlotYMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        else
            set(handles.PlotYMax_edittext, 'BackgroundColor','white');
        end
    case {2,3} % Analysis or TH & Analysis
        if ymax <= 0
            set(handles.PlotYMax_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
        elseif ymax < ymin
            set(handles.PlotYMax_edittext, 'BackgroundColor','red');
            set(handles.PlotYMin_edittext, 'BackgroundColor','red');
            set(handles.Ok_pushbutton,'Enable','Off');
            return
        else
            set(handles.PlotYMax_edittext, 'BackgroundColor','white');
        end
end
if ((isequal(get(handles.PlotXMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotXMax_edittext,'BackgroundColor'),[1,1,1])) & ...
        (isequal(get(handles.PlotYMin_edittext,'BackgroundColor'),[1,1,1]) & ...
        isequal(get(handles.PlotYMax_edittext,'BackgroundColor'),[1,1,1])));
    set(handles.Ok_pushbutton,'Enable','On');
end

handles.yrange(2) = str2num(get(handles.PlotYMax_edittext,'String'));
guidata(hObject,handles);

% --- Executes on button press in Ok_pushbutton.
function Ok_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Ok_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.option = 'ok';

%  Check the entered axis scale values
%  X Axis
% if handles.xrange(2) < 0
guidata(hObject,handles);
uiresume(handles.SelectManualScales_figure);

% --- Executes on button press in Cancel_pushbutton.
function Cancel_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Cancel_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.option = 'cancel';
guidata(hObject,handles);
uiresume(handles.SelectManualScales_figure);